<?php

class Admin_LocationrentController extends ExtLb_Controller_Action{
	
	private $locationTbl;
	private $locationFrm;

    public function init()
    {
        $this->loadCss('admin', 'location', FOLDER_ROOT_NAME);
        $this->loadLayout('admin', 'layout');

        $this->locationTbl = new Admin_Model_Locationrent();
        $this->locationFrm	= new Admin_Form_LocationRent();
    }

    public function indexAction(){
    	if(isset($_GET['search_box_sumit']) && !empty($_GET['search_box_text'])){
    		$selectLocation = $this->locationTbl->selectLoactionRent("khuVuc like '%".$_GET['search_box_text']."%'");
    	}else{
    		$selectLocation = $this->locationTbl->selectLoactionRent();
    	}
    	
    	//
	    //FILTER
	    if(isset($_GET['tbn_filter']) && (int)$_GET['tbn_cate'] != 0){
//	    	$selectLocation = $this->locationTbl->selectLoactionRent("= ".$_GET['tbn_cate']);
	    }

    	$paginatorLocation = new Zend_Paginator(new Zend_Paginator_Adapter_DbSelect($selectLocation));
    	$paginatorLocation->setItemCountPerPage(20)
    					->setPageRange(3)
    					->setCurrentPageNumber($this->_getParam('page',1));
    	//
    	//ADD MENU2
    	if(!isset($_GET['edit'])){
	    	$this->locationFrm->addAction('admin', 'locationrent', 'add');
	    	$this->locationFrm->addSubmitButton('cmdAdd', 'Thêm');
    	}
    	
    	$this->view->paginatorLocation	= $paginatorLocation;
    	$this->view->locationFrm = $this->locationFrm;
    	
    	if (isset($_GET['cmdDel']) && !empty($_GET['menu2-check'])) {
    		$this->_forward('delete','menu2','admin');
    	}
    }

 	public function addAction(){
 		if(isset($_POST['cmdAdd'])){
 			if($this->locationFrm->isValid($this->_request->getPost())){
    			$khuVuc 	= $this->_getParam('txtLocation');
    			$trong  = $this->_getParam('cbbBlank');
    			
    			try {
    				$this->locationTbl->addLocation($khuVuc,$trong);
    				$this->jDialogMessage("Thông báo","Thành công!!!");
    					
    			} catch (Exception $e) {
    				$this->jDialogMessage("Thông báo","Có lỗi xảy ra trong quá trình xử lý. Hãy thử lại với tên khác!!.");
    			}
    			
    			$this->_forward('index','locationrent','admin');
    			$this->_request->clearParams();
    		}
    	}else {
    		$this->_redirect('admin/locationrent/');
    	}
    }
    
	public function  editAction(){
    	//EDIT
    	if($this->locationFrm->isValid($this->_request->getPost())){
    		if (isset($_POST['cmdEdit'])) {
    			$khuVuc 	= $this->_getParam('txtLocation');
    			$trong		= $this->_getParam('cbbBlank');
    			$khuVucId  	= $this->_getParam('txtId');
    			
    			try {
    				$this->locationTbl->updateLocation($khuVuc,$trong, $khuVucId);
    				$this->jDialogMessage("Thông báo","Thành công!!!");
    					
    			} catch (Exception $e) {
    				$this->jDialogMessage("Thông báo","Có lỗi xảy ra trong quá trình xử lý. Hãy thử lại!!.");
    			}
    			
    			$this->_forward('index','locationrent','admin');
    			$this->_request->clearParams();
    		}
    	//
    	//GET INFO TO EDIT
    	}elseif(!empty($_GET['lc']) && isset($_GET['edit'])){
    		$location = $this->locationTbl->fetchAllLocationRent('khuVucId = '.$_GET['lc']);
    		
    		$this->locationFrm->addAction('admin', 'locationrent', 'edit');
    		$this->locationFrm->addSubmitButton('cmdEdit', 'Cập nhập');
    		$this->locationFrm->addHiddenId('txtId', $_GET['lc']);
    		
    		$this->locationFrm->getElement('txtLocation')->setValue($location[0]['khuVuc']);
    		$this->locationFrm->getElement('cbbBlank')->setValue($location[0]['trong']);
    		
    		$this->view->locationFrm = $this->locationFrm;
	    	}else{
	    		$this->_redirect('admin/locationrent/');
	    	}
    }

   	public function deleteAction(){
    	//
    	//DEL ONE RECORD
    	if(isset($_GET['del']) && !empty($_GET['lc'])){
    		
    		try {
    			$this->locationTbl->deleteLocation($_GET['lc']);
    			$this->jDialogMessage("Thông báo","Thành công!!!");	
    		} catch (Exception $e) {
    			$this->jDialogMessage("Thông báo","Có lỗi xảy ra trong quá trình xử lý. Hãy thử lại!!.");
    		}
    		
//    		$this->_forward('index','locationrent','admin');
    	}
    	
    	//DEL MULTI RECORD
    	if(isset($_GET['cmdDel']) ){
        	$arrDataDel = $_GET['location-check'];

        	try {
	        	for($i =0; $i< count($arrDataDel); $i++){
	        		$this->locationTbl->deleteLocation($arrDataDel[$i]);
	        	}
        		
        	} catch (Exception $e) {
        		$this->jDialogMessage("Thông báo","Có lỗi xảy ra trong quá trình xử lý. Hãy thử lại!!.");
        	}
        	
        	$this->_redirect('/admin/locationrent/');
        }
        
        $this->_forward('index','locationrent','admin');
    }
}

